/*
    Project Milestone 3
    by Lorenzo Miori [Lorenzo.Miori@stud-inf.unibz.it,
    Matteo Angriman [Matteo.Angriman@stud-inf.unibz.it],
    Nick Preda [Nicolae.Preda@stud-inf.unibz.it]
*/

package GUI;

import DB.*;
import java.awt.*;
import java.sql.Connection;
import java.util.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author lorenzo
 */
public class ExtraServicesListGUI extends BasicListGUI implements DataModifiedListener {

    private Connection dbConnection;
    private ExtraServiceDB extraServDbConnector;
    /**
     * Creates new form ValidTransactionStatesListGUI
     */
    public ExtraServicesListGUI(Component parentComponent) {
        super(parentComponent);
        this.dbConnection = DatabaseLogin.getLastConnection();
        extraServDbConnector = new ExtraServiceDB(dbConnection);
        setTitle("Extra Services", "Manage your extra services");
        initComponents();
        /* Put the table in the appropriate panel! */
        mainAreaPanel.add(jScrollPane1);
        loadDataInTable();
        finalSetup();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        entryTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        entryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cost", "Description", "Available"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        entryTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(entryTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 530, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable entryTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
    @Override
    public void addEntryAction() {
        HashMap<String, String> newEntry = new HashMap<String, String>();
        newEntry.put(AddressDB.ADDRESS_ID, ""+extraServDbConnector.getUniqueID());
        new ManageExtraServiceGUI(this, newEntry);
        setEnabled(false);
    }

    @Override
    public void editEntryAction() {
        if (entryTable.getSelectedRowCount() > 0) {
            new ManageExtraServiceGUI(this, extraServDbConnector.getTableData().get(entryTable.getSelectedRow()));
            setEnabled(false);
        }
    }

    @Override
    public void removeEntryAction() {
        ArrayList<HashMap<String, String>> entriesData = extraServDbConnector.getTableData();
        if (entryTable.getSelectedRowCount() > 0) {
            int ID = Integer.parseInt(entriesData.get(entryTable.getSelectedRow()).get(HotelDB.HOTEL_ID));
            genericRemoveAction(extraServDbConnector, ID);
            loadDataInTable();
        }
    }

    @Override
    protected void loadDataInTable() {
        cleanTable(entryTable);
        ArrayList<HashMap<String, String>> entriesData = extraServDbConnector.getTableData();
        for (HashMap<String, String> anEntry : entriesData) {
                ((DefaultTableModel) entryTable.getModel()).addRow(new Object[]{
                    anEntry.get(ExtraServiceDB.EXTRASERVICE_ID),
                    anEntry.get(ExtraServiceDB.EXTRASERVICE_COST),
                    anEntry.get(ExtraServiceDB.EXTRASERVICE_DESCRIPTION),
                    (anEntry.get(ExtraServiceDB.EXTRASERVICE_AVAILABILITY).compareTo("t")) == 0 ? true : false
                });
        System.out.println(anEntry);
        }

        entryTable.updateUI();
    }

    @Override
    public void dataModified(int status, HashMap<String, String> newData) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        setEnabled(true);
    }
}
